package edu.xidian.rs.ruisi;

import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import edu.xidian.rs.ruisi.fragment.GerenFragment;
import edu.xidian.rs.ruisi.fragment.LuntanFragment;
import edu.xidian.rs.ruisi.fragment.RetieFragment;
import edu.xidian.rs.ruisi.fragment.ZuijinFragment;


public class MainActivity extends ActionBarActivity implements View.OnClickListener{

    private static final String TAG = "MainActivity";
    private Toolbar mToolbar;
    private int footerMenuNum = 4;
    private LinearLayout btnReTie;
    private LinearLayout btnLunTan;
    private LinearLayout btnZuiJin;
    private LinearLayout btnGeRen;

    private ImageView[] ivFooterArray;
    private TextView[] tvFooterArray;
    private int[] footerImageArray={R.drawable.footer_normal_retie,R.drawable.footer_pressed_retie,
                                    R.drawable.footer_normal_luntan,R.drawable.footer_pressed_luntan,
                                    R.drawable.footer_normal_zuijin,R.drawable.footer_pressed_zuijin,
                                    R.drawable.footer_normal_geren,R.drawable.footer_pressed_geren};

    private FrameLayout mainContent;
//    private Fragment retieFragment;
//    private Fragment luntanFragment;
//    private Fragment zuijinFragment;
//    private Fragment gerenFragment;
    private Fragment[] fragmentArray;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mToolbar = (Toolbar) findViewById(R.id.custom_toolbar);
        mToolbar.setTitle("睿思");
        mToolbar.setTitleTextColor(Color.WHITE);
        mToolbar.setSubtitleTextColor(Color.WHITE);
        setSupportActionBar(mToolbar);

        initContentFragment();
        initMainFooter();
        changeFooterEnable(1);



    }


    private void initContentFragment(){
        mainContent = (FrameLayout) findViewById(R.id.fl_main_content);
        fragmentArray = new Fragment[4];
        fragmentArray[0] = new RetieFragment();
        fragmentArray[1] = new LuntanFragment();
        fragmentArray[2] = new ZuijinFragment();
        fragmentArray[3] = new GerenFragment();

        getSupportFragmentManager().beginTransaction()
                .add(R.id.fl_main_content,fragmentArray[0],"retie")
                .hide(fragmentArray[0])
                .add(R.id.fl_main_content,fragmentArray[1],"luntan")
                .hide(fragmentArray[1])
                .add(R.id.fl_main_content,fragmentArray[2],"zuijin")
                .hide(fragmentArray[2])
                .add(R.id.fl_main_content,fragmentArray[3],"geren")
                .hide(fragmentArray[3])
                .commit();
        getSupportFragmentManager().beginTransaction()
                .show(fragmentArray[0])
                .commit();

    }


    private void initMainFooter(){
        ivFooterArray = new ImageView[footerMenuNum];
        tvFooterArray = new TextView[footerMenuNum];

        btnReTie = (LinearLayout) findViewById(R.id.ll_btn_retie);
        btnLunTan = (LinearLayout) findViewById(R.id.ll_btn_luntan);
        btnZuiJin = (LinearLayout) findViewById(R.id.ll_btn_zuijin);
        btnGeRen = (LinearLayout) findViewById(R.id.ll_btn_geren);

        ivFooterArray[0] = (ImageView) findViewById(R.id.img_footer_retie);
        ivFooterArray[1] = (ImageView) findViewById(R.id.img_footer_luntan);
        ivFooterArray[2] = (ImageView) findViewById(R.id.img_footer_zuijin);
        ivFooterArray[3] = (ImageView) findViewById(R.id.img_footer_geren);

        tvFooterArray[0] = (TextView) findViewById(R.id.tv_footer_retie);
        tvFooterArray[1] = (TextView) findViewById(R.id.tv_footer_luntan);
        tvFooterArray[2] = (TextView) findViewById(R.id.tv_footer_zuijin);
        tvFooterArray[3] = (TextView) findViewById(R.id.tv_footer_geren);


        btnReTie.setOnClickListener(this);
        btnLunTan.setOnClickListener(this);
        btnZuiJin.setOnClickListener(this);
        btnGeRen.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ll_btn_retie:
                changeFooterEnable(0);
                break;
            case R.id.ll_btn_luntan:
                changeFooterEnable(1);
                break;
            case R.id.ll_btn_zuijin:
                changeFooterEnable(2);
                break;
            case R.id.ll_btn_geren:
                changeFooterEnable(3);
                break;
        }
    }



    public void changeFooterEnable(int n){
        FragmentTransaction t = getSupportFragmentManager().beginTransaction();
        for(int i=0;i<footerMenuNum;i++){
            tvFooterArray[i].setTextColor(getResources().getColor(R.color.font_grey));
            ivFooterArray[i].setImageResource(footerImageArray[2*i]);
            t.hide(fragmentArray[i]);
        }
        ivFooterArray[n].setImageResource(footerImageArray[2 * n + 1]);
        tvFooterArray[n].setTextColor(getResources().getColor(R.color.font_blue));
        t.show(fragmentArray[n]);
        t.commit();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
